package edu.umd.ion.wavemeter.server;

import java.io.IOException;

import de.meilhaus.medriver.MeDriver;
import de.meilhaus.medriver.MeIOSingle;

public class MeAnalogOut implements AnalogOut {
	MeDriver Driver = new MeDriver();
	MeIOSingle[] OutArray = new MeIOSingle[1];
	int ChannelOffset = WavemeterConfiguration.getAnalogChannelOffset();
	
	public MeAnalogOut() throws IOException {
		Driver.meOpen(0);
	}
	
	public int digitalValue( double voltage_mv ) {
		return ((int) (0xFFFF * ( voltage_mv + 10000.0 ) / 20000.0)) & 0xFFFF;
	}
	
	@Override
	public void setOutput(int channel, double voltage_mv) throws IOException {
		OutArray[0] = new MeIOSingle(0,channel+ChannelOffset,0,MeDriver.ME_DIR_OUTPUT,
				digitalValue(voltage_mv),
				0,MeDriver.ME_IO_SINGLE_TYPE_NO_FLAGS);
		Driver.meIOSingle(OutArray, MeDriver.ME_IO_SINGLE_CONFIG_NO_FLAGS);
	}

	@Override
	public void close() throws IOException {
		Driver.meClose(0);
	}

}
